package HashTable;

import java.util.HashMap;
import java.util.Map;

public class _409_LongestPalindrome {
    //hint:use map to store the character and int's cont,then loop the map and get get palindrome's count
    //focus:if there are more than one single letter,count+1
    public int longestPalindrome(String s) {
        Map<Character, Integer> map = new HashMap<>();
        char[] array = s.toCharArray();
        for (char c : array) {
            if (map.containsKey(c)) {
                map.put(c, map.get(c) + 1);
            } else {
                map.put(c, 1);
            }
        }
        boolean hasSingle = false;
        int count = 0;
        for (Map.Entry<Character, Integer> m : map.entrySet()) {
            if (m.getValue() % 2 != 0) {
                hasSingle = true;
                count += m.getValue()-1;
            } else {
                count += m.getValue();
            }
        }
        if (hasSingle) {
            count++;
        }
        return count;
    }
}
